<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Common extends Base_Controller 
{
    private static $data = array();
    protected $models=['company_model','project_model','app_common_model','comment_model','collect_model',        
                    'technician_model','file_model'];  
    public function __construct()
    {
        parent::__construct($this->models);        
    }
   
    /**
     *@title 我收藏的店铺
     *@desc 我收藏的店铺
     *@input {"name":"userId","require":"true","type":"int","desc":"用户ID"}
     *@input {"name":"page","type":"string","desc":"页数"}
     *@input {"name":"limit","type":"string","desc":"每页显示条数"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"返回信息;收藏成功/失败"}
     *     
     *@output {"name":"data","type":"array","desc":"店铺数组,下面是详细说明,index代表该数组下标"}
     *@output {"name":"data[index].companyId","desc":"店铺ID","child":"1"}
     *@output {"name":"data[index].poster","desc":"店铺封面","child":"1"}
     *@output {"name":"data[index].name","desc":"店铺名称","child":"1"}
     *@output {"name":"data[index].tradeName","desc":"所属行业","child":"1"}
     *@output {"name":"data[index].judge","desc":"店铺评分分值","child":"1"}  
     *@output {"name":"data[index].consume","desc":"店铺平均消费","child":"1"} 
     * */
    public function myCollect_company(){
        $userId = $this->input->post('userId');
        $page=$this->input->post('page');
        $limit=$this->input->post('limit');
        if(!$userId){
            show3();
        }
        
        $page=empty($page)?0:$page;
        $limit=empty($limit)?20:$limit;
        $offset=$limit*$page;
        
        $company=$this->collect_model->getcollect_company($userId,$limit,$offset);
        if(!$company){            
            show400();
        }     
        
        show200($company);
    }
    
    /**
     *@title 收藏的项目
     *@desc 收藏的项目
     *@input {"name":"userId","require":"true","type":"int","desc":"用户ID"}
     *@input {"name":"page","type":"string","desc":"页数"}
     *@input {"name":"limit","type":"string","desc":"每页显示条数"}
     *    
     *
     *@output {"name":"code","type":"int","desc":"200:获取成功,400获取失败,无数据"}
     *@output {"name":"msg","type":"string","desc":"获取成功/获取失败"}
     *     
     *
     *@output {"name":"data","type":"array","desc":"项目数组","child":"2"} 
     *@output {"name":"data[index].projectId","type":"string","desc":"项目ID","child":"3"}
     *@output {"name":"data[index].title","type":"string","desc":"项目名称","child":"3"}
     *@output {"name":"data[index].poster","type":"string","desc":"项目封面","child":"3"}
     *@output {"name":"data[index].price","type":"string","desc":"项目价格","child":"3"}
     *@output {"name":"data[index].judge","type":"string","desc":"项目评价分值","child":"3"}
     *@output {"name":"data[index].sales","type":"string","desc":"项目销售数量","child":"3"}
     * */
    public function myCollect_project()
    {
        $userId = $this->input->post('userId');
        $page=$this->input->post('page');
        $limit=$this->input->post('limit');
        if(!$userId){
            show3();
        }
        
        $page=empty($page)?0:$page;
        $limit=empty($limit)?20:$limit;
        $offset=$limit*$page;
        
        $data=$this->collect_model->getcollect_project($userId,$limit,$offset);
        
        if(!$data){
            show400();
        }
        
        show200($data);
    }
    
    /**
     *@title 收藏/关注的技师
     *@desc 收藏/关注的技师
     *
     *@input {"name":"userId","require":"true","type":"int","desc":"用户ID"}
     *@input {"name":"page","type":"string","desc":"页数"}
     *@input {"name":"limit","type":"string","desc":"每页显示条数"}
     *
     *
     *@output {"name":"code","type":"int","desc":"200:获取成功,400获取失败,无数据"}
     *@output {"name":"msg","type":"string","desc":"获取成功/获取失败"}
     *
     *@output {"name":"data","type":"array","desc":"技师数组","child":"0"} 
     *@output {"name":"data[index].technician.technicianId","type":"string","desc":"技师ID","child":"1"}
     *@output {"name":"data[index].technician.realName","type":"string","desc":"技师姓名","child":"1"} 
     *@output {"name":"data[index].technician.sevriceNo","type":"string","desc":"技师编号","child":"1"} 
     *@output {"name":"data[index].technician.giftNum","type":"string","desc":"礼物数量","child":"1"} 
     *@output {"name":"data[index].technician.jobName","type":"string","desc":"职业名称,如足疗师","child":"1"}
     *@output {"name":"data[index].technician.judge","type":"string","desc":"评价分值","child":"1"}
     *@output {"name":"data[index].technician.poster","type":"string","desc":"技师封面图片","child":"1"}
     *@output {"name":"data[index].technician.skill","type":"array","desc":"技师的技能数组","child":"1"}
     *@output {"name":"data[index].technician.tags","type":"string","desc":"技师职称,如高级技师","child":"1"}
     *@output {"name":"data[index].technician.workStatus","type":"string","desc":"技师当前状态1:上钟,0空闲","child":"1"}
     * */
    public function myCollect_technician()
    {
        $jobId=$this->input->post('jobId');
        $userId = $this->input->post('userId');
        $page=$this->input->post('page');
        $limit=$this->input->post('limit');
        if(!$userId){
            show3();
        }
        $where=[];
        if($jobId){
            $where['user.jobId']=$jobId;
        }
        $page=empty($page)?0:$page;
        $limit=empty($limit)?20:$limit;
        $offset=$limit*$page;
    
        $technician=$this->collect_model->getcollect_technician($userId,$limit,$offset);
    
        if(!$technician){
            show400();
        }
        
        show200($technician);
    }
    
    /**
     *@title 通用收藏接口
     *@desc 通用收藏接口
     *@input {"name":"listId","require":"true","type":"string","desc":"列表Id(公司Id,职位ID,被关注用户ID)"}
     *@input {"name":"userId","require":"true","type":"int","desc":"用户ID"}
     *@input {"name":"typeFlag","require":"true","type":"int","desc":"收藏类型标记4店铺,5店铺项目,6技师"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息(针对用户),301提示信息(针对开发人员)"}
     *@output {"name":"msg","type":"string","desc":"返回信息;收藏成功/失败"}
     * */
    public function collect(){
        $listId=$this->input->post("listId");
        $userId = $this->input->post('userId');
        $typeFlag=$this->input->post('typeFlag');
        if (!$listId){
            show300('列表ID不正确');
        }
        if(!$userId){
            show3();
        }
        if(!$typeFlag){
            show300('类型标记不能为空');
        }
        $where=['listId'=>$listId,'userId'=>$userId,'typeFlag'=>$typeFlag];
        $colletced=$this->collect_model->getWhereRow($where);
        if ($colletced){
            show300('已经收藏过了');
        }
        $collectData = array(
            'listId' => $listId,
            'userId' => $userId,
            'typeFlag' => $typeFlag
        );
        $bool = $this->collect_model->insert_time( $collectData);
        if ($bool){
            show200([],'收藏成功');
        }else{
            show400('收藏失败');
        }
        
    }
    
    
    /**
     *@title 取消收藏的接口
     *@desc 取消收藏的接口
     *
     *@input {"name":"listId","require":"true","type":"string","desc":"列表Id(公司Id,职位ID,被关注用户ID)"}
     *@input {"name":"userId","require":"true","type":"int","desc":"用户ID"}
     *@input {"name":"typeFlag","require":"true","type":"int","desc":"收藏类型标记4店铺,5店铺项目,6技师"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"返回信息;取消收藏成功/失败"}
     * */
    public function uncollect()
    {
        $listId=$this->input->post("listId");
        $userId = $this->input->post('userId');
        $typeFlag=$this->input->post('typeFlag');
        if (!$listId){
            show300('列表ID不正确');
        }
        if(!$userId){
            show3();
        }
        if(!$typeFlag){
            show300('类型标记不能为空');
        }
        $where=['listId'=>$listId,'userId'=>$userId,'typeFlag'=>$typeFlag];
        $colletced=$this->collect_model->getWhereRow($where);
        if (!$colletced){
            show300('您还未收藏');
        }
        $where=['listId'=>$listId,'userId'=>$userId,'typeFlag'=>$typeFlag];
        $res=$this->collect_model->delWhere($where);
        
        if ($res){
            show200([],'取消收藏成功');
        }else{
            show400('取消收藏失败');
        }
    }
    
    /**
     *@title 通用评论,评价接口
     *@desc 用于对店铺,项目,技师进行评价
     *@input {"name":"listId","require":"true","type":"string","desc":"列表ID,即店铺(公司)ID,项目ID,技师ID"}
     *@input {"name":"userId","type":"int","require":"true","desc":"当前登录用户ID"}
     *@input {"name":"typeFlag","type":"int","require":"true","desc":"类型标记位:0店铺,1项目,2技师"}
     *@input {"name":"judge","type":"int","require":"","desc":"评价分值,不传则默认为0"}
     *@input {"name":"content","type":"string","require":"true","desc":"评论内容"}
     *@input {"name":"images","type":"array","desc":"评论上传的图片,将图片路径数组转化为json字符串传参"}
     *@input {"name":"orderId","type":"int","desc":"订单ID,如果是购买后进行评价,则传此参数,否则不传"}
     *
     *@output {"name":"code","type":"int","desc":"3:未登录,300:一些提示信息,200:成功,400:失败,401:参数有误,"}
     *@output {"name":"msg","type":"string","desc":"评论成功/评论失败"}
     * */
    public function do_comment()
    {                
        $userId = $this->input->get_post('userId');
        $listId = $this->input->get_post('listId');
        $orderId = $this->input->get_post('orderId');
        $content=$this->input->post('content');
        $typeFlag=$this->input->post('typeFlag');
        $judge=$this->input->post('judge');
        $images=$this->input->post('images');
        $images=json_decode($images,true);       //将json字符串转换为数组
        
        if (!$userId){
            show3();
        }
        if(!$listId){
            show401('列表ID错误');
        }
        if(!in_array($typeFlag, [0,1,2])){
            show401('类型参数不正确');
        }
        if(!trim($content)){
            show300('请输入内容');
        }
       
        $data=[
            'listId'=>$listId,
            'userId'=>$userId,
            'content'=>$content,
            'createTime'=>time(),
            'typeFlag'=>$typeFlag,
            'judge'=>empty($judge)?0:$judge
        ];
        if($orderId){
            $data['orderId']=$orderId;
        }
        if ($images){       //有图
            $data['has_image']=1;       //为评论数据加有图标记
        }
        $commentId=$this->comment_model->insert($data);
        if ($commentId){
            //以下这条语句是为了增加评论条数,现因预约和竞拍暂没有此需求所以暂不启用该语句,待有用时再进行修改并启用
            $avgJudge=$this->comment_model->getAvgJudge(['listId'=>$listId,'typeFlag'=>$typeFlag,'judge!='=>0]);
            
            if($typeFlag==0){
                $this->company_model->updateNumPlus(['companyId'=>$listId],'commentNum');       //评论数+1
                $this->company_model->updateWhere(['companyId'=>$listId],['judge'=>$avgJudge]);       //更新平均评价分值
            }elseif ($typeFlag==1){
                $this->project_model->updateNumPlus(['projectId'=>$listId],'commentNum'); 
                $this->project_model->updateWhere(['projectId'=>$listId],['judge'=>$avgJudge]); 
            }elseif ($typeFlag==2){                
                $this->technician_model->updateNumPlus(['technicianId'=>$listId],'commentNum');
                $this->technician_model->updateWhere(['technicianId'=>$listId],['judge'=>$avgJudge]);
            }
            $fileData=[];
            if($images){
                foreach ($images as $key=>$val){
                    $fileData[]=[
                        'listId'=>$commentId,
                        'typeFlag'=>1,
                        'url'=>$val,
                        'createTime'=>time()
                    ];
                }
                $this->file_model->insert_batch($fileData);       //插入评价图片
                
            }
            
            show200([],'评论成功');
        }else{
            show400('评论失败');
        }
    }
    
    /**
     *@title 查看评价列表
     *@desc 查看评价列表,店铺,技师,项目通用
     *@input {"name":"listId","require":"true","type":"string","desc":"列表ID,即店铺(公司)ID,项目ID,技师ID"}
     *@input {"name":"typeFlag","type":"int","require":"true","desc":"类型标记位:0店铺,1项目,2技师"}
     *@input {"name":"flag","type":"int","desc":"筛选标记,不传或传0则表示加载全部评价数据,1表示加载满意的评价数据,2表示加载吐槽的评价数据,3表示有图片的评价数据"}
     *@input {"name":"page","type":"int","desc":"加载页数,从第0页开始,不传则默认为0"}
     *@input {"name":"limit","type":"int","desc":"一页加载的数据条数,不传则默认为20"}
     *
     *@output {"name":"code","type":"int","desc":"3:未登录,300:一些提示信息,200:成功,400:失败,401:参数有误,"}
     *@output {"name":"msg","type":"string","desc":"评论成功/评论失败"}
     *
     *@output {"name":"data","type":"array","desc":"评价数组,下面是详细说明,index代表该数组下标"}
     *@output {"name":"data[index].commentId","desc":"评价ID","child":"1"}
     *@output {"name":"data[index].userId","desc":"用户ID","child":"1"}
     *@output {"name":"data[index].nickName","desc":"用户昵称","child":"1"}
     *@output {"name":"data[index].realName","desc":"用户真实姓名","child":"1"}
     *@output {"name":"data[index].avatar","desc":"用户头像链接","child":"1"}
     *@output {"name":"data[index].content","desc":"评价内容","child":"1"}
     *@output {"name":"data[index].judge","desc":"评价分值","child":"1"}
     *@output {"name":"data[index].images","type":"array","desc":"评价的图片连接数组","child":"1"}
     *@output {"name":"data[index].createTime","type":"array","desc":"评价的时间","child":"1"}
     * */
    public function getComments(){
        $listId=$this->input->post('listId');
        $typeFlag=$this->input->post('typeFlag');
        $page=$this->input->post('page');
        $limit=$this->input->post('limit');
        $flag=$this->input->post('flag');
        if(!$listId){
            show401('列表ID不能为空');
        }
        if(!in_array($typeFlag, [0,1,2])){
            show401('类型参数不符合规定');
        }
        $page=empty($page)?0:$page;
        $limit=empty($limit)?20:$limit;
        $offset=$page*$limit;
        $where=['listId'=>$listId,'typeFlag'=>$typeFlag];
        if($flag==1){
            $where['comment.judge>=']=4;        //满意评价数据
        }elseif($flag==2){
            $where['comment.judge<=']=3;        //吐槽评价数据
        }elseif($flag==3){
            $where['comment.has_image']=1;
        }
        $dbArray['page']=['offset'=>$offset,'limit'=>$limit];
        $dbArray['order']=['createTime'=>'desc'];
        $comments=$this->comment_model->getComments($where,$dbArray);
        if($comments){
            show200($comments);
        }else{
            show400();
        }
        
    }
    
       
    
}
